package com.jeasonzhao.report.engine.impl;

import com.jeasonzhao.commons.parser.expression.BasicEvalProvider;
import com.jeasonzhao.commons.parser.expression.EvalException;
import com.jeasonzhao.commons.parser.expression.ValuePair;
import com.jeasonzhao.report.engine.ReportUserInfo;

public class UserEvalProvider extends BasicEvalProvider
{
    private static final String SYMBOL_PREFIX = "user.";
    private ReportUserInfo m_userInfo = null;
    public UserEvalProvider(ReportUserInfo user)
    {
        super(true); //Case insensitive
        m_userInfo = user;
    }

    public ValuePair evalVariable(String strExpression)
        throws EvalException
    {
        try
        {
            return super.evalVariable(strExpression);
        }
        catch(Exception ex)
        {

        }
        if(null == m_userInfo)
        {
            throw new EvalException("Can not found current user��");
        }
        strExpression = strExpression.trim().toLowerCase();
        if(strExpression.startsWith(SYMBOL_PREFIX.toLowerCase()) == false)
        {
            throw new EvalException("Can not handler the symbol:" + strExpression +
                                    "," + UserEvalProvider.class.getName() + " can only handle symbols which starts with " + SYMBOL_PREFIX);
        }
        strExpression = strExpression.substring(SYMBOL_PREFIX.length());
        return new ValuePair(m_userInfo.getProperty(strExpression));
    }
}
